<template>
  <transition name="modal-fade" @after-leave="closed">
    <div v-show="show" class="base-modal-mask" @click.self="maskClose">
      <div
        :class="{
          'base-modal': true,
          transition__container: true,
          'base-modal-fullscreen': isFullscreen,
        }"
        :style="{ width: width, 'max-height': '100%', margin: '0' }"
      >
        <!-- 弹窗头部——Start -->
        <div class="base-modal-header">
          <slot name="header">
            <div class="title">
              <slot name="title">
                <h3 v-if="title">
                  {{ title }}
                </h3>
              </slot>
              <slot name="information"> </slot>
            </div>
            <button
              type="button"
              v-if="allowFullscreen"
              @click="isFullscreen = !isFullscreen"
            >
              <i class="iconfont icon-quanping"></i>
            </button>
            <button v-if="isDownload" type="button">
              <i class="iconfont iconColor icon-cloud-download"></i>
            </button>
            <button
              type="button"
              class="base-modal-header-close"
              @click="cancel"
              v-if="closeable"
            >
              <i class="iconfont iconColor icon-fe-close"></i>
            </button>
          </slot>
        </div>
        <!-- 弹窗头部——End -->
        <div :class="['Video_Body', bodyClass]">
          <slot></slot>
        </div>

        <div class="base-modal-footer" v-if="$slots.footer">
          <slot name="footer"></slot>
        </div>
      </div>
    </div>
  </transition>
</template>

<script>
import {
  getScrollBarWidth,
  hasClass,
  removeClass,
  addClass,
  getStyle,
} from "@src/util/dom";

export default {
  name: "base-modal-pdf",
  props: {
    show: {
      // 是否显示组件
      type: Boolean,
      default: false,
    },
    title: {
      type: String,
      default: "",
    },
    isDownload: {
      type: Boolean,
      default: false,
    },
    width: {
      type: String,
      default: "640px",
    },
    closeable: {
      // 是否能手动关闭
      type: Boolean,
      default: true,
    },
    maskCloseable: {
      // 是否允许点击遮罩关闭
      type: Boolean,
      default: false,
    },
    bodyClass: String,
    appendToBody: {
      // 是否将弹窗插入body中
      type: Boolean,
      default: false,
    },
    /** 是否允许全屏 */
    allowFullscreen: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      isFullscreen: false,
    };
  },
  watch: {
    show(val) {
      if (val) return this.computeStyle();
      this.restoreBodyStyle();
      this.isFullscreen = false;
    },
  },
  methods: {
    closed() {
      this.$emit("closed"); // 关闭动画结束时触发
    },
    close() {
      this.$emit("close"); // 关闭时触发
      // 兼容sync
      this.$emit("update:show", false);
    },
    cancel() {
      this.$emit("cancel"); // 点击关闭按钮，或遮罩层关闭时触发
      this.close();
    },
    maskClose() {
      if (this.closeable && this.maskCloseable) {
        this.cancel();
      }
    },
    escClose(event) {
      if (this.show && this.closeable && event.keyCode == 27) {
        this.cancel();
      }
    },
    computeStyle() {
      this.withoutHiddenClass = !hasClass(
        document.body,
        "overflow-body-for-modal"
      );
      this.bodyPaddingRight = document.body.style.paddingRight;
      let computedBodyPaddingRight = parseInt(
        getStyle(document.body, "paddingRight"),
        10
      );
      let bodyHasOverflow =
        document.documentElement.clientHeight < document.body.scrollHeight;
      let bodyOverflowY = getStyle(document.body, "overflowY");
      let scrollBarWidth = getScrollBarWidth();

      if (
        scrollBarWidth > 0 &&
        (bodyHasOverflow || bodyOverflowY === "scroll") &&
        this.withoutHiddenClass
      ) {
        document.body.style.paddingRight = `${
          computedBodyPaddingRight + scrollBarWidth
        }px`;
      }
      addClass(
        document.body,
        `${document.body.className} overflow-body-for-modal`
      );
    },
    restoreBodyStyle() {
      if (!this.show && this.withoutHiddenClass) {
        document.body.style.paddingRight = this.bodyPaddingRight;
        removeClass(document.body, "overflow-body-for-modal");
      }
      this.withoutHiddenClass = true;
    },
  },
  mounted() {
    if (this.appendToBody) {
      document.body.appendChild(this.$el);
    }
  },
  destroyed() {
    // if appendToBody is true, remove DOM node after destroy
    if (this.appendToBody && this.$el && this.$el.parentNode) {
      this.$el.parentNode.removeChild(this.$el);
    }
  },
};
</script>

<style lang="scss" scoped>
.overflow-body-for-modal {
  overflow: hidden;
}

.base-modal-mask {
  @include mask();
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 3000;
  overflow: auto;
}

.base-modal {
  position: relative;
  margin: 50px auto;
  background-color: transparent;
  border-radius: 1px;
  box-shadow: 1px 1px 8px rgba(0, 0, 0, 0.15);

  overflow: hidden;

  height: 100%;

  display: flex;
  flex-direction: column;

  &.base-modal-fullscreen {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: 0;
    width: auto !important;
  }
}

.base-modal-header {
  background-color: #32373a;
  min-height: 44px;
  position: relative;
  padding: 10px 15px 10px 20px;
  font-size: 16px;
  color: #ffffff;
  font-weight: normal;

  display: flex;
  align-items: center;
  .title {
    flex: 1 0 auto;
    display: flex;
    justify-content: center;
  }
  h3 {
    display: inline-block;
    flex: 0 1 auto;
    margin: 0;
    height: 24px;
    line-height: 24px;
    font-size: 16px;
    font-weight: 600;
    font-size: 18px;
    max-width: 300px;
    @include text-ellipsis();
  }

  button {
    height: 24px;
    line-height: 24px;
    width: 24px;

    padding: 0;
    margin: 0;
    outline: none;
    color: #666;
    border: none;
    background-color: transparent;
    transition: color ease 0.15s;

    i {
      font-size: 14px;
    }

    &:hover {
      @include fontColor();
    }

    &.base-modal-header-close:hover {
      color: #e84040;
    }
  }

  button + button {
    margin-left: 5px;
  }
}

.base-modal-body {
  flex: 1;
  overflow: auto;
}

//TODO: 统一边距
.base-modal-footer {
  min-height: 52px;
  display: flex;
  justify-content: flex-end;
  padding: 10px 20px;
  border-top: 1px solid $color-border-l2;
}

.base-modal-text-btn {
  outline: none;
  border: none;
  background-color: transparent;
  color: $text-color-secondary;

  &:hover {
    background-color: #f0f0f0;
  }

  &:disabled {
    cursor: not-allowed;
  }

  & + button {
    margin-left: 10px;
  }
}
.Video_Body {
  display: flex;
  flex: 1 0 auto;
  width: 100%;
}
.iconColor {
  color: #ffffff !important;
  font-size: 16px !important;
}
</style>
